package com.navi168.fwq21.util;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.za.communication.core.codec.decoder.DataSegmentationDecoder;
import org.za.communication.core.codec.encoder.DataCombineEncoder;
import org.za.communication.core.handler.SessionIdleHandler;
import org.za.communication.message.GetListFromSer;
import org.za.communication.message.GetVerifyCodeMessage;
import org.za.communication.message.Loginmessage;
import org.za.communication.message.TxlMessage;

import com.navi168.fwq21.R;
import com.navi168.fwq21.R.layout;
import com.navi168.fwq21.friend.FriendAct;
import com.navi168.fwq21.hyzl.ShowMap;
import com.navi168.fwq21.monitoring.Monitoring;
import com.navi168.fwq21.settings.Settingindex;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainPage extends Activity {
	// private ImageButton hyzlButton;
	private View hyzl;
	private View hylb;
	private View qchy;
	private View rjsz;
	private SessionIdleHandler sessionIdleHandler = new SessionIdleHandler();
	private String md5 = "";
	private String md5f = "";
	private String md5c = "";
	private String txlcString;
	private IoSession session = sessionIdleHandler.ioSession;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = this.getApplicationContext();
		 
		findViews();
		setListers();
		Intent intent = getIntent();

		md5 = intent.getStringExtra("md5");
		md5f = intent.getStringExtra("md5f");
		md5c = intent.getStringExtra("md5c");
		txlcString = intent.getStringExtra("txlc");

		checkMd5s();

	}

	private void setListers() {
		hyzl.setOnClickListener(showmapListener);
		hylb.setOnClickListener(friendlistListener);
		qchy.setOnClickListener(qichelistListener);
		rjsz.setOnClickListener(rjszlistener);
	}

	private void findViews() {
		hyzl = findViewById(R.id.hyzl_m_layout);
		hylb = findViewById(R.id.hylb_m_layout);
		qchy = findViewById(R.id.qchy_m_layout);
		rjsz = findViewById(R.id.sz_m_layout);

	}

	Button.OnClickListener showmapListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent it = new Intent(MainPage.this, ShowMap.class);
			startActivity(it);

		}
	};

	Button.OnClickListener friendlistListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent it = new Intent(MainPage.this, FriendAct.class);
			startActivity(it);
		}
	};

	Button.OnClickListener qichelistListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent it = new Intent(MainPage.this, Monitoring.class);
			startActivity(it);

		}
	};

	Button.OnClickListener rjszlistener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent it = new Intent(MainPage.this, Settingindex.class);
			startActivity(it);

		}
	};

	private void checkMd5s() {
		SharedPreferences settings = mContext.getSharedPreferences(
				Constants.SPF, 0);
		 SharedPreferences.Editor editor = settings.edit();
		String md5t = sessionIdleHandler.md5s[0];
		String md5friend = sessionIdleHandler.md5s[1];
		String md5car = sessionIdleHandler.md5s[2];
		if (!md5t.equals(md5)) {
			udloadTxl();
		}else if(!md5friend.equals(md5f)){

			editor.putString("friendlistmd5", md5t);
			editor.commit();
			getfriendlist();
			while (true) {
				if (sessionIdleHandler.returnlist.length()>0||sessionIdleHandler.returnlist.equals("")) {
					String friendlist = sessionIdleHandler.returnlist;
					Log.i(Constants.TAG,"friendlist "+friendlist);
					break;
				}
			}
			
		}if (!md5car.equals(md5c)) {

			editor.putString("carlistmd5", md5t);
			editor.commit();
			getcarlist();
			while (true) {
				if (sessionIdleHandler.returnlist.length()>0||sessionIdleHandler.returnlist.equals("")) {
					String carlist = sessionIdleHandler.returnlist;
					Log.i(Constants.TAG,"carlist "+carlist);
					break;
				}
			}
			
		}
		
	}


	/**
	 * 返回服务器端的车友友列表。
	 */
	private void getcarlist() {
		GetListFromSer getmessage = new GetListFromSer();
		getmessage.setType(2);
		session.write(getmessage);
		
	}

	/**
	 * 返回服务器端的好友列表。
	 */
	private void getfriendlist() {
		GetListFromSer getmessage = new GetListFromSer();
		getmessage.setType(1);
		session.write(getmessage);
		
	}

	private void udloadTxl() {

		TxlMessage txlMessage = new TxlMessage();
		byte[] key = new byte[4];
		key = sessionIdleHandler.key;
		txlMessage.setKey(key);
		txlMessage.setTxlc(txlcString);
		session.write(txlMessage);
		Log.i(Constants.TAG, "上传通讯录！");
	}

}
